package cn.tedu.opencv.controller;

import cn.tedu.opencv.service.ImgDetectionService;
import cn.tedu.opencv.ex.Result;
import cn.tedu.opencv.params.RemoveParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/imgDetection")
@Api(tags = "图像检测")
@Validated
public class ImgDetectionController {

    @Qualifier("custom")
    @Autowired
    private ImgDetectionService imgDetectionService;

    @ApiOperation(value = "(1)先将相似分组，再找出每个分组中最清晰")
    @PostMapping("/detect")
    @ApiOperationSupport(order = 10)
    public Result<List<String>> detect(@RequestPart("files") List<MultipartFile> files,
                                       @RequestParam(value = "selectPath", defaultValue = "C:\\similarImgs") String selectPath,
                                       @RequestParam(value = "percentage", defaultValue = "0.8") double percentage) {
        return Result.success(imgDetectionService.detect(files, selectPath + "\\", percentage));
    }

    @ApiOperation(value = "(2.1)将相似分组，并分组输出到文件夹")
    @PostMapping("/similar-groups")
    @ApiOperationSupport(order = 20)
    public Result<List<String>> groupSimilarImages(@RequestPart("files") List<MultipartFile> files,
                                                   @RequestParam(value = "selectPath", defaultValue = "C:\\similarImgs") String selectPath,
                                                   @RequestParam(value = "percentage", defaultValue = "0.8") double percentage) {
        return Result.success(imgDetectionService.group(files, selectPath + "\\", percentage));
    }


    @ApiOperation(value = "(2.2)找出每个分组中最清晰")
    @PostMapping("/clearest-in-groups")
    @ApiOperationSupport(order = 30)
    public Result<List<String>> findClearestInEveryGroup(@RequestParam(value = "folderPath", defaultValue = "C:\\similarImgs") String folderPath) {
        return Result.success(imgDetectionService.findClearestInGroups(folderPath));
    }

//    @ApiOperation(value = "找出最清晰")
//    @PostMapping("/clearest")
//    public Result<String> clearest(@RequestPart("files") List<MultipartFile> files) {
//        return Result.success(imgDetectionService.findClearest(files));
//    }

    @ApiOperation(value = "(3)去除模糊图片")
    @PostMapping("/remove-blurry-images")
    @ApiOperationSupport(order = 40)
    public Result<String> remove(@Valid RemoveParam removeParam) {
        return Result.success(imgDetectionService.removeBlurryImages(removeParam));
    }

//    @ApiOperation(value = "(4)测试")
//    @PostMapping("/remove-images")
//    @ApiOperationSupport(order = 50)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "folderPath", value = "请选择空白的输出文件夹", required = true, defaultValue = "C:\\similarImgs")
//    })
//    public Result<List<String>> removeTest(@RequestPart("files") List<MultipartFile> files,
//                                           @RequestParam(value = "folderPath", defaultValue = "C:\\similarImgs") String folderPath) {
//        return Result.success(imgDetectionService.findClearestInGroups(null));
//    }

}
